package com.francis.springbootcommonutil.number;

import java.math.BigDecimal;

/**
 * <p>Title: CalculateUtil</p>
 * <p>Description: CalculateUtil  四则运算  加 减 乘 除</p>
 * <p>Copyright: Xi An BestTop Technologies, ltd. Copyright(c) 2018/p>
 *
 * @author ChenWei
 * @version 0.0.0.1
 * <pre>Histroy:
 *       2020/6/22 16:17 Create by ChenWei
 * </pre>
 */
public class CalculateUtil {

    /**
     * @methodName add
     * @description 精确的 加法运算
     * @param v1, v2, number
     * @return java.lang.String
     * @author ChenWei
     * @date 2020/6/22 16:11
     */
    public static double add(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(v1 + "");
        BigDecimal b2 = new BigDecimal(v2 + "");
        return b1.add(b2).doubleValue();
    }
    public static String add(String v1, String v2, int scale ) {
        BigDecimal b1 = new BigDecimal(v1 + "");
        BigDecimal b2 = new BigDecimal(v2 + "");
        return b1.add(b2).setScale(scale,BigDecimal.ROUND_HALF_UP).toString();
    }
    public static BigDecimal add(BigDecimal v1, BigDecimal v2, int number ) {
        return v1.add(v2).setScale(number,BigDecimal.ROUND_HALF_UP);
    }


    /**
     * @methodName add
     * @description 精确的 减法运算
     * @param v1, v2, number
     * @return java.lang.String
     * @author ChenWei
     * @date 2020/6/22 16:11
     */
    public static double subtract(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.subtract(b2).doubleValue();
    }

    public static double subtract(String v1, String v2, int number) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.subtract(b2).setScale(number,BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static String subtractStr(String v1, String v2, int number) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.subtract(b2).setScale(number,BigDecimal.ROUND_HALF_UP).toString();
    }


    /**
     * @methodName add
     * @description 提供相对精确的 乘法运算
     * @param v1, v2, number
     * @return java.lang.String
     * @author ChenWei
     * @date 2020/6/22 16:11
     */
    public static Double multiply(double v1, double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1 + "");
        BigDecimal b2 = new BigDecimal(v2 + "");
        return b1.multiply(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    public static String multiplyNum(double v1, double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1 + "");
        BigDecimal b2 = new BigDecimal(v2 + "");
        return b1.multiply(b2).setScale(scale,BigDecimal.ROUND_HALF_UP).toString();
    }
    public static String multiplyStr(String v1, String v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.multiply(b2).setScale(scale,BigDecimal.ROUND_HALF_UP).toString();
    }
    public static Double multiplyBigDecimal(BigDecimal v1, BigDecimal v2,int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        return v1.multiply(v2).setScale(scale,BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static BigDecimal multiply(BigDecimal v1, BigDecimal v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        return v1.multiply(v2).setScale(scale,BigDecimal.ROUND_HALF_UP);
    }


    /**
     * @methodName add
     * @description 提供相对精确的 除法运算    保留位：四舍五入
     * @param v1, v2, number
     * @return java.lang.String
     * @author ChenWei
     * @date 2020/6/22 16:11
     */
    public static double divide(double v1, double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1 + "");
        BigDecimal b2 = new BigDecimal(v2 + "");
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static double divide(String v1, String v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1 );
        BigDecimal b2 = new BigDecimal(v2 );
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static String divideStr(String v1, String v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1 );
        BigDecimal b2 = new BigDecimal(v2 );
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).toString();
    }

    /**
     * @methodName add
     * @description 提供相对精确的 除法运算    保留位：向下取整.
     * @param v1, v2, number
     * @return java.lang.String
     * @author ChenWei
     * @date 2020/6/22 16:11
     */
    public static String divideStr(String v1, String v2) {
        BigDecimal b1 = new BigDecimal(v1 );
        BigDecimal b2 = new BigDecimal(v2 );
        return b1.divide(b2, 0, BigDecimal.ROUND_DOWN).toString();
    }

}
